package com.cpi.cornpp.futuresversions.services;

import java.util.ArrayList;
import java.util.List;

import com.cpi.cornpp.common.exception.CornPPException;
import com.cpi.cornpp.futuresversions.converter.CornFuturesVersionsConverter;
import com.cpi.cornpp.futuresversions.dao.CornFuturesVersionsDAO;
import com.cpi.cornpp.futuresversions.dto.CornFuturesVersionsDTO;
import com.cpi.cornpp.futuresversions.exception.CornFuturesVersionsException;
import com.cpi.cornpp.futuresversions.view.CornFuturesVersionsView;

/**
 * Corn Futures Versions Service
 * @author Edgar Joao
 *
 */
public class CornFuturesVersionsService {

	/**
	 * Get List Of Corn Futures Versions
	 * @param orderField
	 * @param ascendingOrder
	 * @return
	 * @throws CornFuturesVersionsException
	 */
	public List listCornFuturesVersions(int orderField, boolean ascendingOrder, int versionId) throws CornFuturesVersionsException{
		CornFuturesVersionsDAO futuresVersionsDAO = null;
		CornFuturesVersionsConverter cornFuturesVersionsConverter = null;
		List cornFuturesVersionsDTO = null;
		List cornFuturesVersionsView = null;
		
		try{
			futuresVersionsDAO = new CornFuturesVersionsDAO();
			cornFuturesVersionsConverter = new CornFuturesVersionsConverter();
			cornFuturesVersionsDTO = new ArrayList(0);
			cornFuturesVersionsView = new ArrayList(0);
			
			cornFuturesVersionsDTO = futuresVersionsDAO.listFuturesVersions(orderField, ascendingOrder, versionId);
			cornFuturesVersionsView = cornFuturesVersionsConverter.convertViews(cornFuturesVersionsDTO);
			
			
		}catch (CornFuturesVersionsException cornFuturesVersionsException) {
			cornFuturesVersionsException.printStackTrace(System.out);
			cornFuturesVersionsException = new CornFuturesVersionsException(cornFuturesVersionsException, CornPPException.LAYER_SERVICE, CornPPException.ACTION_LISTS);
			throw cornFuturesVersionsException;
		}
		return cornFuturesVersionsView;
	}
	
	
	/**
	 * Add a new Corn Futures Versions
	 * @author Edgar Joao
	 * @param view
	 * @throws CornFuturesVersionsException
	 */
	public void addCornFuturesVersions(CornFuturesVersionsView view) throws CornFuturesVersionsException{
		CornFuturesVersionsDAO futuresVersionsDAO = null;
		CornFuturesVersionsConverter cornFuturesVersionsConverter = null;
		CornFuturesVersionsDTO cornFuturesVersionsDTO = null;
				
			try {
				futuresVersionsDAO = new CornFuturesVersionsDAO();
				cornFuturesVersionsConverter = new CornFuturesVersionsConverter();
				cornFuturesVersionsDTO = cornFuturesVersionsConverter.converterCornFuturesVersionsViewToDTO(view);
				
				futuresVersionsDAO.insertCornFuturesVersions(cornFuturesVersionsDTO);
				
			} catch (CornFuturesVersionsException cornFuturesVersionsException) {
				cornFuturesVersionsException.printStackTrace(System.out);
				cornFuturesVersionsException= new CornFuturesVersionsException(cornFuturesVersionsException, CornPPException.LAYER_SERVICE,CornPPException.ACTION_INSERT);
				throw cornFuturesVersionsException;
			}catch (Exception e){
				CornFuturesVersionsException purchaseProgramException = null;
				e.printStackTrace(System.out);
				purchaseProgramException = new CornFuturesVersionsException(e , CornPPException.LAYER_SERVICE,CornPPException.ACTION_INSERT);
				throw purchaseProgramException;
			}			
	}
	
	/**
	 * Update a Corn Futures Versions
	 * @author Edgar Joao 
	 * @param view
	 * @throws CornFuturesVersionsException
	 */
	public void updateCornFuturesVersions(CornFuturesVersionsView view) throws CornFuturesVersionsException {
		CornFuturesVersionsDAO futuresVersionsDAO = null;
		CornFuturesVersionsConverter cornFuturesVersionsConverter = null;
		CornFuturesVersionsDTO cornFuturesVersionsDTO = null;
				
		try {
			futuresVersionsDAO = new CornFuturesVersionsDAO();
			cornFuturesVersionsConverter = new CornFuturesVersionsConverter();
			cornFuturesVersionsDTO = cornFuturesVersionsConverter.converterCornFuturesVersionsViewToDTO(view);
			
			futuresVersionsDAO.updateCornFuturesVersions(cornFuturesVersionsDTO);
			
		} catch (CornFuturesVersionsException cornFuturesVersionsException) {
			cornFuturesVersionsException.printStackTrace(System.out);
			cornFuturesVersionsException= new CornFuturesVersionsException(cornFuturesVersionsException, CornPPException.LAYER_SERVICE,CornPPException.ACTION_UPDATE);
			throw cornFuturesVersionsException;
		}catch (Exception e){
			CornFuturesVersionsException purchaseProgramException = null;
			e.printStackTrace(System.out);
			purchaseProgramException = new CornFuturesVersionsException(e , CornPPException.LAYER_SERVICE,CornPPException.ACTION_UPDATE);
			throw purchaseProgramException;
		}			
	}
	
	/**
	 * Exist a Corn Futures Versions
	 * @author Edgar Joao
	 * @param view
	 * @return
	 * @throws CornFuturesVersionsException
	 */
	public boolean existCornFuturesVersions(CornFuturesVersionsView view) throws CornFuturesVersionsException{
		CornFuturesVersionsDAO futuresVersionsDAO = null;
		CornFuturesVersionsConverter cornFuturesVersionsConverter = null;
		CornFuturesVersionsDTO cornFuturesVersionsDTO = null;
		boolean exist = false;
				
		try {
			futuresVersionsDAO = new CornFuturesVersionsDAO();
			cornFuturesVersionsConverter = new CornFuturesVersionsConverter();
			cornFuturesVersionsDTO = cornFuturesVersionsConverter.converterCornFuturesVersionsViewToDTO(view);
			
			if(futuresVersionsDAO.existCornFuturesVersions(cornFuturesVersionsDTO)){
				exist = true;
			}else{
				exist = false;
			}
			
		} catch (CornFuturesVersionsException cornFuturesVersionsException) {
			cornFuturesVersionsException.printStackTrace(System.out);
			cornFuturesVersionsException= new CornFuturesVersionsException(cornFuturesVersionsException, CornPPException.LAYER_SERVICE,CornPPException.ACTION_SELECT);
			throw cornFuturesVersionsException;
		}catch (Exception e){
			CornFuturesVersionsException purchaseProgramException = null;
			e.printStackTrace(System.out);
			purchaseProgramException = new CornFuturesVersionsException(e , CornPPException.LAYER_SERVICE,CornPPException.ACTION_SELECT);
			throw purchaseProgramException;
		}
		return exist;
	}
	
	
	/**
	 * Delete a Corn Futures Versions
	 * @author Edgar Joao
	 * @param view
	 * @throws CornFuturesVersionsException
	 */
	public void deleteCornFuturesVersions(int versionId, int cornFutureVersionId) throws CornFuturesVersionsException{
		CornFuturesVersionsDAO futuresVersionsDAO = null;
		try {
			futuresVersionsDAO = new CornFuturesVersionsDAO();
			futuresVersionsDAO.deleteCornFuturesVersions(versionId, cornFutureVersionId);
		} catch (CornFuturesVersionsException cornFuturesVersionsException) {
			cornFuturesVersionsException.printStackTrace(System.out);
			cornFuturesVersionsException= new CornFuturesVersionsException(cornFuturesVersionsException, CornPPException.LAYER_SERVICE,CornPPException.ACTION_DELETE);
			throw cornFuturesVersionsException;
		}catch (Exception e){
			CornFuturesVersionsException purchaseProgramException = null;
			e.printStackTrace(System.out);
			purchaseProgramException = new CornFuturesVersionsException(e , CornPPException.LAYER_SERVICE,CornPPException.ACTION_DELETE);
			throw purchaseProgramException;
		}	
	}
	
	/**
	 * Get a Corn Futures Versions
	 * @author Edgar Joao
	 * @param view
	 * @return
	 * @throws CornFuturesVersionsException
	 */
	public CornFuturesVersionsView getCornFuturesVersionsView(CornFuturesVersionsView view) throws CornFuturesVersionsException{
		CornFuturesVersionsDAO futuresVersionsDAO = null;
		CornFuturesVersionsConverter cornFuturesVersionsConverter = null;
		CornFuturesVersionsDTO cornFuturesVersionsDTO = null;
		CornFuturesVersionsView futuresVersionsView = null;
				
		try {
			futuresVersionsDAO = new CornFuturesVersionsDAO();
			cornFuturesVersionsConverter = new CornFuturesVersionsConverter();
			futuresVersionsView = new CornFuturesVersionsView();
			
			cornFuturesVersionsDTO = cornFuturesVersionsConverter.converterCornFuturesVersionsViewToDTO(view);						
			cornFuturesVersionsDTO = futuresVersionsDAO.getCornFuturesVersions(cornFuturesVersionsDTO);			
			futuresVersionsView = cornFuturesVersionsConverter.converterCornFuturesVersionsDtoToView(cornFuturesVersionsDTO);
			
		}catch (CornFuturesVersionsException cornFuturesVersionsException) {
			cornFuturesVersionsException.printStackTrace(System.out);
			cornFuturesVersionsException= new CornFuturesVersionsException(cornFuturesVersionsException, CornPPException.LAYER_SERVICE,CornPPException.ACTION_SELECT);
			throw cornFuturesVersionsException;
		}catch (Exception e){
			CornFuturesVersionsException purchaseProgramException = null;
			e.printStackTrace(System.out);
			purchaseProgramException = new CornFuturesVersionsException(e , CornPPException.LAYER_SERVICE,CornPPException.ACTION_SELECT);
			throw purchaseProgramException;
		}
		return futuresVersionsView;
	}
	
	/**
	 * List Corn Futures Periods Versions
	 * @author Edgar Joao
	 * @return
	 * @throws CornFuturesVersionsException
	 */
	public List listCornFuturesPeriodsVersion(int versionId) throws CornFuturesVersionsException{
		CornFuturesVersionsDAO futuresVersionsDAO = null;						
		List arrayResult = null; 		
		
		try {
			
			futuresVersionsDAO = new CornFuturesVersionsDAO();			
			arrayResult = new ArrayList(0);			
			arrayResult = futuresVersionsDAO.listCornFuturesPeriodsVersion(versionId);
			
		}catch (CornFuturesVersionsException cornFuturesVersionsException) {
			cornFuturesVersionsException.printStackTrace(System.out);
			cornFuturesVersionsException= new CornFuturesVersionsException(cornFuturesVersionsException, CornPPException.LAYER_SERVICE,CornPPException.ACTION_SELECT);
			throw cornFuturesVersionsException;
		}catch (Exception e){
			CornFuturesVersionsException purchaseProgramException = null;
			e.printStackTrace(System.out);
			purchaseProgramException = new CornFuturesVersionsException(e , CornPPException.LAYER_SERVICE,CornPPException.ACTION_SELECT);
			throw purchaseProgramException;
		}
		return arrayResult;
	}
	
	/**
	 * List Corn Futures Periods Versions
	 * @author Edgar Joao
	 * @return
	 * @throws CornFuturesVersionsException
	 */
	public List cornFuturesPeriodsVersion(int versionId) throws CornFuturesVersionsException{
		CornFuturesVersionsDAO futuresVersionsDAO = null;						
		List arrayResultDTOs = null;
		List arrayResultViews = null;
		CornFuturesVersionsConverter cornFuturesVersionsConverter = null;
		try {
			cornFuturesVersionsConverter = new CornFuturesVersionsConverter();
			futuresVersionsDAO = new CornFuturesVersionsDAO();			
			arrayResultDTOs = new ArrayList(0);			
			arrayResultDTOs = futuresVersionsDAO.listCornFuturesPeriodsVersion(versionId);
			arrayResultViews = cornFuturesVersionsConverter.convertViewsToDTO(arrayResultDTOs);
		}catch (CornFuturesVersionsException cornFuturesVersionsException) {
			cornFuturesVersionsException.printStackTrace(System.out);
			cornFuturesVersionsException= new CornFuturesVersionsException(cornFuturesVersionsException, CornPPException.LAYER_SERVICE,CornPPException.ACTION_SELECT);
			throw cornFuturesVersionsException;
		}catch (Exception e){
			CornFuturesVersionsException purchaseProgramException = null;
			e.printStackTrace(System.out);
			purchaseProgramException = new CornFuturesVersionsException(e , CornPPException.LAYER_SERVICE,CornPPException.ACTION_SELECT);
			throw purchaseProgramException;
		}
		return arrayResultViews;
	}
}